package com.lunwen.lunwen.api;

import com.lunwen.lunwen.bean.Result;
import com.lunwen.lunwen.bean.ReturnXueSheng;
import com.lunwen.lunwen.bean.XueSheng;
import com.lunwen.lunwen.config.ShiroUtils;
import com.lunwen.lunwen.services.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * (XueSheng)表控制层
 *
 * @author makejava
 * @since 2020-07-20 12:24:51
 */
@RestController
@RequestMapping("api")
public class XueShengController {

    /**
     * 服务对象
     */
    @Autowired
    private XueShengService xueShengService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private XueYuanService xueYuanService;

    @Autowired
    private ZhuanYeService zhuanYeService;

    @Autowired
    private BanJiService banJiService;

    @RequestMapping("registxuesheng")
    public Result registXueSheng(@RequestBody XueSheng xueSheng) {
        Result result = new Result();
        if (StringUtils.isEmpty(xueSheng.getSysUser().getUsername())){
            result.setResult("201","用户名不能为空！");
            return result;
        }
        if (StringUtils.isEmpty(xueSheng.getSysUser().getPassword())){
            result.setResult("201","用户密码不能为空！");
            return result;
        }
        if (sysUserService.queryByName(xueSheng.getSysUser().getUsername()) != null){
            result.setResult("201","该用户已存在！");
            return result;
        }

        if (!StringUtils.isEmpty(xueSheng.getXueyuanId())){
            if (xueYuanService.queryById(xueSheng.getXueyuanId()) == null){
                result.setResult("201","该学院不存在！");
                return result;
            }
        }else{
            result.setResult("201","学院ID不能为空！");
            return result;
        }
        if (!StringUtils.isEmpty(xueSheng.getZhuanyeId())){
            if (zhuanYeService.queryById(xueSheng.getZhuanyeId()) == null){
                result.setResult("201","该专业不存在！");
                return result;
            }
            if (!zhuanYeService.queryById(xueSheng.getZhuanyeId()).getXueyuanId().equals(xueSheng.getXueyuanId())){
                result.setResult("201","该学院不存在该专业！");
                return result;
            }
        }else{
            result.setResult("201","专业ID不能为空！");
            return result;
        }
        if (!StringUtils.isEmpty(xueSheng.getBanjiId())){
            if (banJiService.queryById(xueSheng.getBanjiId()) == null){
                result.setResult("201","该班级不存在！");
                return result;
            }
            if (!banJiService.queryById(xueSheng.getBanjiId()).getZhuanyeId().equals(xueSheng.getZhuanyeId())){
                result.setResult("201","该专业不存在该班级！");
                return result;
            }
        }else{
            result.setResult("201","班级ID不能为空！");
            return result;
        }

        xueSheng.setId(UUID.randomUUID().toString());
        xueSheng.getSysUser().setSalt("123");
        xueSheng.getSysUser().setPassword(ShiroUtils.sha256(xueSheng.getSysUser().getPassword(),xueSheng.getSysUser().getSalt()));
        xueSheng.getSysUser().setStatus(1);
        xueSheng.getSysUser().setType(1);
        xueSheng.getSysUser().setCreateUserId(0l);
        xueSheng.getSysUser().setCreateTime(new Date());
        XueSheng insert = xueShengService.insert(xueSheng);
        result.setResult("200","注册成功！",insert);
        return result;
    }

    @RequestMapping("selectonexuesheng")
    public Result selectOneXueSheng(@RequestParam("username") String username){
        Result result = new Result();
        XueSheng xueSheng = xueShengService.queryByUserName(username);
        System.out.println(xueSheng);
        if (xueSheng == null){
            result.setResult("201","不存在该学生！");
            return result;
        }
        ReturnXueSheng returnXueSheng = new ReturnXueSheng();
        returnXueSheng.setXueyuanId(xueSheng.getXueyuanId());
        returnXueSheng.setXueyuanmingcheng(xueYuanService.queryById(xueSheng.getXueyuanId()).getXueyuanmingcheng());
        returnXueSheng.setZhuanyeId(xueSheng.getZhuanyeId());
        returnXueSheng.setZhuanyemingcheng(zhuanYeService.queryById(xueSheng.getZhuanyeId()).getZhuanyemingcheng());
        returnXueSheng.setBanjiId(xueSheng.getBanjiId());
        returnXueSheng.setBanjimingcheng(banJiService.queryById(xueSheng.getBanjiId()).getBanjimingcheng());
        returnXueSheng.setUserId(xueSheng.getUserId());
        returnXueSheng.setUsername(xueSheng.getSysUser().getUsername());
        returnXueSheng.setTruename(xueSheng.getSysUser().getTruename());
        returnXueSheng.setMobile(xueSheng.getSysUser().getMobile());
        returnXueSheng.setQq(xueSheng.getSysUser().getQq());
        returnXueSheng.setEmail(xueSheng.getSysUser().getEmail());
        returnXueSheng.setStatus(xueSheng.getSysUser().getStatus());
        returnXueSheng.setCreateTime(xueSheng.getSysUser().getCreateTime());
        result.setResult("200","查询成功！",returnXueSheng);
        return result;
    }

    @RequestMapping("selectallxuesheng")
    public Result selectAllXueSheng(@RequestParam("yeMa") int yeMa, @RequestParam(required = false) String id, @RequestParam(required = false, defaultValue = "0") int type){
        Result result = new Result();
        XueSheng xueSheng1 = new XueSheng();
        xueSheng1.setUserId(-1l);
        if (type == 1){
            xueSheng1.setBanjiId(id);
        }else if (type == 2){
            xueSheng1.setZhuanyeId(id);
        }else if (type == 3){
            xueSheng1.setXueyuanId(id);
        }
        int limit = 10;
        int zongGeShu = xueShengService.queryCount(xueSheng1);
        int xiaBiao = (yeMa - 1) * limit;
        int zongYeMa = (zongGeShu - 1) / limit + 1;
        List<XueSheng> xueShengs = xueShengService.queryAllByLimit(xueSheng1, xiaBiao, limit);
        List<ReturnXueSheng> returnXueShengs = new ArrayList<ReturnXueSheng>();
        ReturnXueSheng returnXueSheng;
        for(XueSheng xueSheng : xueShengs){
            returnXueSheng = new ReturnXueSheng();
            returnXueSheng.setXueyuanId(xueSheng.getXueyuanId());
            returnXueSheng.setXueyuanmingcheng(xueYuanService.queryById(xueSheng.getXueyuanId()).getXueyuanmingcheng());
            returnXueSheng.setZhuanyeId(xueSheng.getZhuanyeId());
            returnXueSheng.setZhuanyemingcheng(zhuanYeService.queryById(xueSheng.getZhuanyeId()).getZhuanyemingcheng());
            returnXueSheng.setBanjiId(xueSheng.getBanjiId());
            returnXueSheng.setBanjimingcheng(banJiService.queryById(xueSheng.getBanjiId()).getBanjimingcheng());
            returnXueSheng.setUserId(xueSheng.getUserId());
            returnXueSheng.setUsername(xueSheng.getSysUser().getUsername());
            returnXueSheng.setTruename(xueSheng.getSysUser().getTruename());
            returnXueSheng.setMobile(xueSheng.getSysUser().getMobile());
            returnXueSheng.setQq(xueSheng.getSysUser().getQq());
            returnXueSheng.setEmail(xueSheng.getSysUser().getEmail());
            returnXueSheng.setStatus(xueSheng.getSysUser().getStatus());
            returnXueSheng.setCreateTime(xueSheng.getSysUser().getCreateTime());
            returnXueShengs.add(returnXueSheng);
        }
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("zongGeShu", zongGeShu);
        map.put("zongYeMa", zongYeMa);
        map.put("list", returnXueShengs);
        result.setResult("200","查询成功！",map);
        return result;
    }

    @RequestMapping("updatexuesheng")
    public Result updateXueSheng(@RequestBody XueSheng xueSheng) {
        Result result = new Result();
        if (StringUtils.isEmpty(xueSheng.getUserId())){
            result.setResult("201","用户ID不能为空！");
            return result;
        }
        if (xueShengService.queryByUserId(xueSheng.getUserId()) == null){
            result.setResult("201","该学生不存在！");
            return result;
        }
        if (!StringUtils.isEmpty(xueSheng.getSysUser().getUsername())){
            if (sysUserService.queryByName(xueSheng.getSysUser().getUsername()) != null){
                result.setResult("201","用户名已存在！");
                return result;
            }
        }
        if (!StringUtils.isEmpty(xueSheng.getXueyuanId())){
            if (xueYuanService.queryById(xueSheng.getXueyuanId()) == null){
                result.setResult("201","该学院不存在！");
                return result;
            }
        }else{
            result.setResult("201","学院ID不能为空！");
            return result;
        }
        if (!StringUtils.isEmpty(xueSheng.getZhuanyeId())){
            if (zhuanYeService.queryById(xueSheng.getZhuanyeId()) == null){
                result.setResult("201","该专业不存在！");
                return result;
            }
            if (!zhuanYeService.queryById(xueSheng.getZhuanyeId()).getXueyuanId().equals(xueSheng.getXueyuanId())){
                result.setResult("201","该学院不存在该专业！");
                return result;
            }
        }else{
            result.setResult("201","专业ID不能为空！");
            return result;
        }
        if (!StringUtils.isEmpty(xueSheng.getBanjiId())){
            if (banJiService.queryById(xueSheng.getBanjiId()) == null){
                result.setResult("201","该班级不存在！");
                return result;
            }
            if (!banJiService.queryById(xueSheng.getBanjiId()).getZhuanyeId().equals(xueSheng.getZhuanyeId())){
                result.setResult("201","该专业不存在该班级！");
                return result;
            }
        }else{
            result.setResult("201","班级ID不能为空！");
            return result;
        }
        if (!StringUtils.isEmpty(xueSheng.getSysUser().getPassword())){
            xueSheng.getSysUser().setPassword(ShiroUtils.sha256(xueSheng.getSysUser().getPassword(),"123"));
        }
        if (xueSheng.getSysUser().getStatus() != null){
            if (xueSheng.getSysUser().getStatus() != 0L && xueSheng.getSysUser().getStatus() != 1L){
                result.setResult("201","状态信息错误！");
                return result;
            }
        }
        xueSheng = xueShengService.update(xueSheng);
        ReturnXueSheng returnXueSheng = new ReturnXueSheng();
        returnXueSheng.setXueyuanId(xueSheng.getXueyuanId());
        returnXueSheng.setXueyuanmingcheng(xueYuanService.queryById(xueSheng.getXueyuanId()).getXueyuanmingcheng());
        returnXueSheng.setZhuanyeId(xueSheng.getZhuanyeId());
        returnXueSheng.setZhuanyemingcheng(zhuanYeService.queryById(xueSheng.getZhuanyeId()).getZhuanyemingcheng());
        returnXueSheng.setBanjiId(xueSheng.getBanjiId());
        returnXueSheng.setBanjimingcheng(banJiService.queryById(xueSheng.getBanjiId()).getBanjimingcheng());
        returnXueSheng.setUserId(xueSheng.getUserId());
        returnXueSheng.setUsername(xueSheng.getSysUser().getUsername());
        returnXueSheng.setTruename(xueSheng.getSysUser().getTruename());
        returnXueSheng.setMobile(xueSheng.getSysUser().getMobile());
        returnXueSheng.setQq(xueSheng.getSysUser().getQq());
        returnXueSheng.setEmail(xueSheng.getSysUser().getEmail());
        returnXueSheng.setStatus(xueSheng.getSysUser().getStatus());
        returnXueSheng.setCreateTime(xueSheng.getSysUser().getCreateTime());
        result.setResult("200","修改成功！",xueSheng);
        return result;
    }

}